package com.ruoyi.project.purchase.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.domain.ResultBean;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.RyDepotHead;
import com.ruoyi.project.purchase.domain.RyQuality;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.mapper.RyDepotDetailMapper;
import com.ruoyi.project.purchase.mapper.RyDepotHeadMapper;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.warehouse.domain.RyDepot;
import com.ruoyi.project.warehouse.domain.RyDepotItem;
import com.ruoyi.project.warehouse.domain.RyInventoryCurrentStock;
import com.ruoyi.project.warehouse.mapper.RyDepotItemMapper;
import com.ruoyi.project.warehouse.mapper.RyDepotMapper;
import com.ruoyi.project.warehouse.mapper.RyInventoryCurrentStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.purchase.mapper.RyPurchaseInMapper;
import com.ruoyi.project.purchase.domain.RyPurchaseIn;
import com.ruoyi.project.purchase.service.IRyPurchaseInService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 采购入库Service业务层处理
 *
 * @author you
 * @date 2022-09-26
 */
@Service
public class RyPurchaseInServiceImpl implements IRyPurchaseInService
{
    @Autowired
    private RyPurchaseInMapper ryPurchaseInMapper;

    @Autowired
    private RyDepotHeadMapper ryDepotHeadMapper;

    @Autowired
    private RyDepotDetailMapper ryDepotDetailMapper;

    @Autowired
    private RyDepotMapper ryDepotMapper;

    @Autowired
    private RyDepotItemMapper ryDepotItemMapper;

    @Autowired
    private RyInventoryCurrentStockMapper ryInventoryCurrentStockMapper;

    /**
     * 查询采购入库
     *
     * @param id 采购入库主键
     * @return 采购入库
     */
    @Override
    public RyPurchaseIn selectRyPurchaseInById(Long id)
    {
        return ryPurchaseInMapper.selectRyPurchaseInById(id);
    }

    /**
     * 查询单据主表及子表信息
     *
     * @param id 单据主表主键
     * @return 单据主表及子表信息
     */
    @Override
    public DepotHeadItem selectRyDepotDetailByDepotId(Long id){
        DepotHeadItem depotHeadItem = new DepotHeadItem();
        RyPurchaseIn ryPurchaseIn = ryPurchaseInMapper.selectRyPurchaseInById(id);

        //主表数据
        depotHeadItem.setId(ryPurchaseIn.getId());

        depotHeadItem.setNumber(ryPurchaseIn.getNumber());//单据号
        depotHeadItem.setContractNumber(ryPurchaseIn.getContractNumber());//采购合同号

        depotHeadItem.setCreator(ryPurchaseIn.getCreator());//操作员
        depotHeadItem.setOperName(ryPurchaseIn.getOperName());//操作员名称

        depotHeadItem.setCreateTime(ryPurchaseIn.getCreateTime());
        depotHeadItem.setOperTime(ryPurchaseIn.getOperTime());
        depotHeadItem.setFileName(ryPurchaseIn.getFileName());
        depotHeadItem.setSalesMan(ryPurchaseIn.getSalesMan());

        depotHeadItem.setLinkNumber(ryPurchaseIn.getLinkNumber());//关联订单号
        depotHeadItem.setLinkHeadId(ryPurchaseIn.getLinkHeadId());//关联订单id
        depotHeadItem.setRemark(ryPurchaseIn.getRemark());//备注

        //子表数据
        List<RyDepotItem> tableData = new ArrayList<RyDepotItem>();
        if(ryPurchaseIn.getLinkHeadId() != null){//关联采购订单不为空
            //将入库单id和采购订单id作为参数传入查询
            //子表数据
            tableData = ryDepotItemMapper.selectPurchaseInItemByDepotId(id,ryPurchaseIn.getLinkHeadId());
        } else {//不关联采购订单，则无需查询采购订单信息和质检信息
            //子表数据
            tableData = ryDepotItemMapper.selectOrderItemByDepotId(id);
            for(RyDepotItem ryDepotItem : tableData){
                ryDepotItem.setActualPurchaseNum(ryDepotItem.getOperNumber());
            }
        }

        //遍历子表数据，关联出入库记录表查询已入库数量，如果没有记录则设置已入库数量为0
//        for(RyDepotDetail ryDepotDetail : tableData){
//            //设置查询参数为入库仓库id和物品id
//            RyDepotItem ryDepotItem = new RyDepotItem();
//            ryDepotItem.setDepotId(ryDepotDetail.getInDepotId());//入库仓库id
//            ryDepotItem.setInventoryId(ryDepotDetail.getInventoryId());//物品id
//            //查询已入库数量，在查询过程中已做了处理：如果没有入库记录则查询结果为null，自动将null转为0
//            Long haveInNum = ryDepotItemMapper.getNumByDepotInventoryId(ryDepotItem);
//            ryDepotDetail.setHavaInNum(haveInNum);
//
//        }
        depotHeadItem.setTableData(tableData);
        return depotHeadItem;
    }


    /**
     * 新增采购入库
     * 实质上只新增了采购入库主表，子表数据只需要修改相应信息即可
     * 根据主表修改单据子表信息
     * 1.同时需要生成入库记录
     * 2.更改项目库物品当前库存数
     * 3.设置采购总清单入库数量
     */
    @Override
    @Transactional
    public AjaxResult addDepotHeadAndDetail(DepotHeadItem depotHeadItem){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //新增采购入库单主表信息
        RyPurchaseIn ryPurchaseIn = new RyPurchaseIn();
        ryPurchaseIn.setNumber(depotHeadItem.getNumber());//单据号
        ryPurchaseIn.setFileName(depotHeadItem.getFileName());
        ryPurchaseIn.setSalesMan(depotHeadItem.getSalesMan());
        ryPurchaseIn.setCreateTime(DateUtils.getNowDate());//创建时间
        ryPurchaseIn.setCreator(user.getUserId());//操作员
        ryPurchaseIn.setRemark(depotHeadItem.getRemark());
        ryPurchaseIn.setStatus("0");
        ryPurchaseIn.setType("入库");
        ryPurchaseIn.setSubType("采购");

        //判断是否关联了采购订单
        if(depotHeadItem.getLinkHeadId() != null) {//关联了采购订单
            ryPurchaseIn.setLinkHeadId(depotHeadItem.getLinkHeadId());//关联采购订单id
            ryPurchaseIn.setLinkNumber(depotHeadItem.getNumber());
            //查询采购单信息
            RyDepotHead ryDepotHead = ryDepotHeadMapper.selectRyDepotHeadById(depotHeadItem.getLinkHeadId());
            if(ryDepotHead.getProjectHeadId() != null){//如果采购订单中存在项目总清单，则关联了项目
                ryPurchaseIn.setProjectHeadId(ryDepotHead.getProjectHeadId());
            }

        }

        /**
         * 统计参数，用于统计明细入库数量
         * 如果totalInNum > 0 存在入库明细，则不需处理
         * 如果totalInNum <= 0 说明明细数据因为条件问题一条都没有入库，
         * 这时就主动抛出错误，使得事务生效，防止主表信息插入而明细表没有数据
         *
         */
        int totalInNum = 0;

        //主表信息入库
        int res = ryPurchaseInMapper.insertRyPurchaseIn(ryPurchaseIn);
        if(res > 0){

            /**
             * 遍历明细数据，更新单据明细数据
             * 1.生成相应入库记录
             * 2.更改项目库物品当前库存数
             */
            for(RyDepotItem ryDepotItem : tableData){

                ryDepotItem.setHeaderId(ryPurchaseIn.getId());//设置刚生成的采购入库单id
                ryDepotItem.setHeaderType("0");//0代表入库数据
                ryDepotItem.setOperTime(new Date());
                ryDepotItem.setUpdateTime(new Date());

//                //如果合格数量为0或是为null,或是操作数量operNumber为0或者为null，则跳过此次循环，不进行入库操作
//                if(ryDepotItem.getQualifiedNum() == null || ryDepotItem.getQualifiedNum() == 0 || ryDepotItem.getOperNumber() == null || ryDepotItem.getOperNumber() == 0){
//                    continue;
//                }

                //如果操作数量operNumber为0或者为null，则跳过此次循环，不进行入库操作
                if(ryDepotItem.getOperNumber() == null || ryDepotItem.getOperNumber() == 0){
                    continue;
                }

                //如果入库仓库不存在，则跳过此次循环，不进行入库操作
                if(ryDepotItem.getDepotId() == null){
                    continue;
                }

                //获取仓库id和商品id
                Long depotId = ryDepotItem.getDepotId();
                Long inventoryId = ryDepotItem.getInventoryId();
                RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
                paramstock.setDepotId(depotId);
                paramstock.setInventoryId(inventoryId);
                /**
                 * 修改项目库中当前库存
                 * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
                 * 1.存在的话就对当前量进行修改
                 * 2.不存在则在仓库当前库存记录中新增此条记录
                 * 3.不论仓库当前库存表中是否存在此记录，都需要在表单明细子表中新增此条入库记录
                 */
                //查询是否存在
                RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
                System.out.println("--> result:"+result);

                //创建参数对象
                RyInventoryCurrentStock param = new RyInventoryCurrentStock();
                if(result != null){//存在此数据，执行修改
                    //计算需要更改的数量
                    //获取原库存数
                    Double oldNum = result.getCurrentNumber();
                    //获取新入库数量
                    Double inNum = ryDepotItem.getOperNumber();
                    //需要更改的数量
                    Double updateNum = oldNum + inNum;
                    param.setCurrentNumber(updateNum);

                    //执行修改语句，根据id修改
                    param.setId(result.getId());
                    ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
                } else {//不存在,执行新增
                    param.setDepotId(depotId);
                    param.setInventoryId(inventoryId);
                    param.setCurrentNumber(ryDepotItem.getOperNumber());

                    //执行新增语句
                    ryInventoryCurrentStockMapper.insertRyInventoryCurrentStock(param);
                }

                //执行明细新增
                ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
                totalInNum += 1;

            }
        }

        //如果统计结果小于等于0,则主动抛出异常
        if(totalInNum <= 0){
            throw new RuntimeException("入库数据出现入库数量为空或其他问题");
//            return AjaxResult.error("入库数据出现入库数量为空或其他问题");
        }
        return AjaxResult.success("新增成功");

    }

    /**
     * 备份原始新增方法
     */
    public void oldAdd(){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //新增采购入库单主表信息
        RyPurchaseIn ryPurchaseIn = new RyPurchaseIn();
        DepotDetailBean depotDetailBean = new DepotDetailBean();
        //判断是否有子表数据
        List<RyDepotDetail> tableData = depotDetailBean.getTableData();
        //判断是否关联了采购订单
        if(depotDetailBean.getLinkHeadId() != null){//关联了采购订单
            ryPurchaseIn.setLinkHeadId(depotDetailBean.getLinkHeadId());//关联采购订单id
            //主表信息入库
            int res1 = ryPurchaseInMapper.insertRyPurchaseIn(ryPurchaseIn);
            if(res1 > 0){

                /**
                 * 遍历明细数据，更新单据明细数据
                 * 1.生成相应入库记录
                 * 2.更改项目库物品当前库存数
                 * 3.设置采购总清单明细表中入库数量
                 */
                for(RyDepotDetail ryDepotDetail : tableData){
                    //关联采购入库单
                    ryDepotDetail.setPurchaseInId(ryPurchaseIn.getId());

                    //如果合格数量为0或是为null,或是操作数量operNumber为0或者为null，则跳过此次循环，不进行入库操作
                    if(ryDepotDetail.getQualifiedNum() == null || ryDepotDetail.getQualifiedNum() == 0 || ryDepotDetail.getOperNumber() == null || ryDepotDetail.getOperNumber() == 0){
                        continue;
                    }

                    //如果入库仓库不存在，则跳过此次循环，不进行入库操作
                    if(ryDepotDetail.getInDepotId() == null){
                        continue;
                    }

                    //获取仓库id和商品id
                    Long depotId = ryDepotDetail.getInDepotId();
                    Long inventoryId = ryDepotDetail.getInventoryId();
                    RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
                    paramstock.setDepotId(depotId);
                    paramstock.setInventoryId(inventoryId);
                    /**
                     * 修改项目库中当前库存
                     * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
                     * 1.存在的话就对当前量进行修改
                     * 2.不存在则在仓库当前库存记录中新增此条记录
                     * 3.不论仓库当前库存表中是否存在此记录，都需要在表单明细子表中新增此条入库记录
                     */
                    //查询是否存在
                    RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
                    System.out.println("--> result:"+result);

                    //创建参数对象
                    RyInventoryCurrentStock param = new RyInventoryCurrentStock();
                    if(result != null){//存在此数据，执行修改
                        //计算需要更改的数量
                        //获取原库存数
                        Double oldNum = result.getCurrentNumber();
                        //获取新入库数量
                        Double inNum = ryDepotDetail.getOperNumber();
                        //需要更改的数量
                        Double updateNum = oldNum + inNum;
                        param.setCurrentNumber(updateNum);

                        //执行修改语句，根据id修改
                        param.setId(result.getId());
                        ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
                    } else {//不存在,执行新增
                        param.setDepotId(depotId);
                        param.setInventoryId(inventoryId);
                        param.setCurrentNumber(ryDepotDetail.getOperNumber());

                        //执行新增语句
                        ryInventoryCurrentStockMapper.insertRyInventoryCurrentStock(param);
                    }
                    //入库记录新增
                    RyDepotItem ryDepotItem = new RyDepotItem();
                    ryDepotItem.setInventoryId(inventoryId);
                    ryDepotItem.setDepotId(depotId);
                    ryDepotItem.setOperNumber(ryDepotDetail.getOperNumber());
                    ryDepotItem.setOperTime(DateUtils.getNowDate());
                    ryDepotItem.setOperMan(user.getUserId());
                    ryDepotItem.setPurchaseInId(ryPurchaseIn.getId());//设置采购入库单id
                    ryDepotItem.setHeaderType("0");//0代表入库
                    //新增
                    ryDepotItemMapper.insertRyDepotItem(ryDepotItem);

                    //修改采购总清单明细表中总入库数量（已入库数量+本次入库操作数量）
                    ryDepotDetail.setInNum(ryDepotDetail.getHavaInNum() + ryDepotDetail.getOperNumber());
                    ryDepotDetailMapper.updateRyDepotDetail(ryDepotDetail);
                }

            }
        } else {//不关联采购订单
            int res2 = ryPurchaseInMapper.insertRyPurchaseIn(ryPurchaseIn);
            if(res2 > 0){
                /**
                 * 遍历明细数据，更新单据明细数据
                 * 1.生成相应入库记录
                 * 2.更改项目库物品当前库存数
                 * 3.设置采购总清单明细表中入库数量
                 */
                for(RyDepotDetail ryDepotDetail : tableData){
                    //关联采购入库单
                    ryDepotDetail.setPurchaseInId(ryPurchaseIn.getId());

                    /**
                     * 如果不关联采购订单
                     * 则此入库单就没有采购数量、合格数量等采购和质检环节字段
                     * 所以下方只需要判断是否存在入库数量operNum即可
                     */
                    //如果操作数量operNumber为0或者为null，则跳过此次循环，不进行入库操作
                    if(ryDepotDetail.getOperNumber() == null || ryDepotDetail.getOperNumber() == 0){
                        continue;
                    }

                    //如果入库仓库不存在，则跳过此次循环，不进行入库操作
                    if(ryDepotDetail.getInDepotId() == null){
                        continue;
                    }

                    //获取仓库id和商品id
                    Long depotId = ryDepotDetail.getInDepotId();
                    Long inventoryId = ryDepotDetail.getInventoryId();
                    RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
                    paramstock.setDepotId(depotId);
                    paramstock.setInventoryId(inventoryId);
                    /**
                     * 修改项目库中当前库存
                     * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
                     * 1.存在的话就对当前量进行修改
                     * 2.不存在则在仓库当前库存记录中新增此条记录
                     * 3.不论仓库当前库存表中是否存在此记录，都需要在表单明细子表中新增此条入库记录
                     */
                    //查询是否存在
                    RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
                    System.out.println("--> result:"+result);

                    //创建参数对象
                    RyInventoryCurrentStock param = new RyInventoryCurrentStock();
                    if(result != null){//存在此数据，执行修改
                        //计算需要更改的数量
                        //获取原库存数
                        Double oldNum = result.getCurrentNumber();
                        //获取新入库数量
                        Double inNum = ryDepotDetail.getOperNumber();
                        //需要更改的数量
                        Double updateNum = oldNum + inNum;
                        param.setCurrentNumber(updateNum);

                        //执行修改语句，根据id修改
                        param.setId(result.getId());
                        ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
                    } else {//不存在,执行新增
                        param.setDepotId(depotId);
                        param.setInventoryId(inventoryId);
                        param.setCurrentNumber(ryDepotDetail.getOperNumber());

                        //执行新增语句
                        ryInventoryCurrentStockMapper.insertRyInventoryCurrentStock(param);
                    }
                    //入库记录新增
                    RyDepotItem ryDepotItem = new RyDepotItem();
                    ryDepotItem.setInventoryId(inventoryId);
                    ryDepotItem.setDepotId(depotId);
                    ryDepotItem.setOperNumber(ryDepotDetail.getOperNumber());
                    ryDepotItem.setOperTime(DateUtils.getNowDate());
                    ryDepotItem.setOperMan(user.getUserId());
                    ryDepotItem.setPurchaseInId(ryPurchaseIn.getId());//设置采购入库单id
                    ryDepotItem.setHeaderType("0");//0代表入库
                    //新增
                    ryDepotItemMapper.insertRyDepotItem(ryDepotItem);

                    //修改采购总清单明细表中总入库数量（已入库数量+本次入库操作数量）
                    ryDepotDetail.setInNum(ryDepotDetail.getHavaInNum() + ryDepotDetail.getOperNumber());
                    ryDepotDetailMapper.updateRyDepotDetail(ryDepotDetail);
                }
            }
        }
    }

    /**
     * 根据主表修改单据子表信息
     * 1.同时需要生成入库记录
     * 2.更改项目库物品当前库存数
     * 3.设置采购总清单入库数量
     */
    @Transactional
    @Override
    public AjaxResult editDepotDetail(DepotHeadItem depotHeadItem){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //判断是否有子表数据
        List<RyDepotItem> tableData = depotHeadItem.getTableData();
        if(tableData == null || tableData.size() ==0){
            return AjaxResult.error("子表数据不存在");
        }

        //采购入库模块只需要修改个别信息即可
        RyPurchaseIn ryPurchaseIn = new RyPurchaseIn();
        ryPurchaseIn.setId(depotHeadItem.getId());//主键，用于定位修改数据
        ryPurchaseIn.setOperTime(DateUtils.getNowDate());
        ryPurchaseIn.setFileName(depotHeadItem.getFileName());
        ryPurchaseIn.setRemark(depotHeadItem.getRemark());
        //修改
        ryPurchaseInMapper.updateRyPurchaseIn(ryPurchaseIn);

        /**
         * 首先查询采购入库单关联明细信息，保存下来后处理仓库当前数量
         * 逻辑：
         * 1.将之前新增的数据保存下来，每条数据都有operNumber代表了入库数量
         * 2.仓库中的当前数量是新增之后的数量
         * 3.在删除数据重新插入前需要将仓库中的当前数量还原为新增前的数量
         * 4.遍历保存下来的数据，在循环中还原仓库当前数量（当前数量 - operNumber,和新增逻辑相反）
         */
        List<RyDepotItem> oldData = ryDepotItemMapper.selectRyDepotItemByDepotId(depotHeadItem.getId());
        //遍历之前新增的数据，还原仓库当前数量
        for(RyDepotItem ryDepotItem : oldData){
            //获取仓库id和商品id
            Long depotId = ryDepotItem.getDepotId();
            Long inventoryId = ryDepotItem.getInventoryId();
            RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
            paramstock.setDepotId(depotId);
            paramstock.setInventoryId(inventoryId);
            /**
             * 修改项目库中当前库存
             * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
             * 1.存在的话就对当前量进行修改
             * 2.不存在则不处理
             */
            //查询是否存在
            RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
            System.out.println("--> result:"+result);

            //创建参数对象
            RyInventoryCurrentStock param = new RyInventoryCurrentStock();
            if(result != null) {//存在此数据，执行修改
                //计算需要更改的数量
                //获取原库存数
                Double oldNum = result.getCurrentNumber();
                //获取入库数量
                Double inNum = ryDepotItem.getOperNumber();
                //需要更改的数量
                Double updateNum = oldNum - inNum;
                param.setCurrentNumber(updateNum);

                //执行修改语句，根据id修改
                param.setId(result.getId());
                ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
            }

        }

        //将子表数据先删除，根据主表id删除
        ryDepotItemMapper.deleteDepotItemByHeadId(depotHeadItem.getId());

        /**
         * 统计参数，用于统计明细入库数量
         * 如果totalInNum > 0 存在入库明细，则不需处理
         * 如果totalInNum <= 0 说明明细数据因为条件问题一条都没有入库，
         * 这时就主动抛出错误，使得事务生效，防止主表信息插入而明细表没有数据
         *
         */
        int totalInNum = 0;

        //遍历明细数据，新增单据明细数据
        for(RyDepotItem ryDepotItem : tableData){

            ryDepotItem.setHeaderId(ryPurchaseIn.getId());//设置采购入库单id
            ryDepotItem.setHeaderType("0");//0代表入库数据
            ryDepotItem.setOperTime(new Date());
            ryDepotItem.setUpdateTime(new Date());

            //如果操作数量operNumber为0或者为null，则跳过此次循环，不进行入库操作
            if(ryDepotItem.getOperNumber() == null || ryDepotItem.getOperNumber() == 0){
                continue;
            }

            //如果入库仓库不存在，则跳过此次循环，不进行入库操作
            if(ryDepotItem.getDepotId() == null){
                continue;
            }

            //获取仓库id和商品id
            Long depotId = ryDepotItem.getDepotId();
            Long inventoryId = ryDepotItem.getInventoryId();
            RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
            paramstock.setDepotId(depotId);
            paramstock.setInventoryId(inventoryId);
            /**
             * 修改项目库中当前库存
             * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
             * 1.存在的话就对当前量进行修改
             * 2.不存在则在仓库当前库存记录中新增此条记录
             * 3.不论仓库当前库存表中是否存在此记录，都需要在表单明细子表中新增此条入库记录
             */
            //查询是否存在
            RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
            System.out.println("--> result:"+result);

            //创建参数对象
            RyInventoryCurrentStock param = new RyInventoryCurrentStock();
            if(result != null){//存在此数据，执行修改
                //计算需要更改的数量
                //获取原库存数
                Double oldNum = result.getCurrentNumber();
                //获取新入库数量
                Double inNum = ryDepotItem.getOperNumber();
                //需要更改的数量
                Double updateNum = oldNum + inNum;
                param.setCurrentNumber(updateNum);

                //执行修改语句，根据id修改
                param.setId(result.getId());
                ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
            } else {//不存在,执行新增
                param.setDepotId(depotId);
                param.setInventoryId(inventoryId);
                param.setCurrentNumber(ryDepotItem.getOperNumber());

                //执行新增语句
                ryInventoryCurrentStockMapper.insertRyInventoryCurrentStock(param);
            }

            //执行明细新增
            ryDepotItemMapper.insertRyDepotItem(ryDepotItem);
            totalInNum += 1;

        }

        //如果统计结果小于等于0,则主动抛出异常
        if(totalInNum <= 0){
            System.out.println("应主动抛出异常,稍后完善");
        }

        return AjaxResult.success("修改成功");
    }




    /**
     * --采购入库
     * 单条数据入库
     */
    @Transactional
    @Override
    public AjaxResult insertOneDetail(RyDepotDetail ryDepotDetail){
        //获取用户信息
        SysUser user = SecurityUtils.getLoginUser().getUser();

        //如果合格数量为0或是为null,或是操作数量operNumber为0或者为null，不进行入库操作
        if(ryDepotDetail.getQualifiedNum() == null || ryDepotDetail.getQualifiedNum() == 0 || ryDepotDetail.getOperNumber() == null || ryDepotDetail.getOperNumber() == 0){
            return AjaxResult.error("合格数量或入库数量为空");
        }

        //如果入库仓库不存在，则跳过此次循环，不进行入库操作
        if(ryDepotDetail.getInDepotId() == null){
            return AjaxResult.error("入库仓库不存在");
        }

        //获取仓库id和商品id
        Long depotId = ryDepotDetail.getInDepotId();
        Long inventoryId = ryDepotDetail.getInventoryId();
        RyInventoryCurrentStock paramstock = new RyInventoryCurrentStock();
        paramstock.setDepotId(depotId);
        paramstock.setInventoryId(inventoryId);
        /**
         * 修改项目库中当前库存
         * 根据仓库id和商品id查询仓库当前库存记录中是否存在此物品记录，
         * 1.存在的话就对当前量进行修改
         * 2.不存在则在仓库当前库存记录中新增此条记录
         * 3.不论仓库当前库存表中是否存在此记录，都需要在表单明细子表中新增此条入库记录
         */
        //查询是否存在
        RyInventoryCurrentStock result = ryInventoryCurrentStockMapper.getCurrentStockByDepotInventId(paramstock);
        System.out.println("--> result:"+result);

        //创建参数对象
        RyInventoryCurrentStock param = new RyInventoryCurrentStock();
        if(result != null){//存在此数据，执行修改
            //计算需要更改的数量
            //获取原库存数
            Double oldNum = result.getCurrentNumber();
            //获取新入库数量
            Double inNum = ryDepotDetail.getOperNumber();
            //需要更改的数量
            Double updateNum = oldNum + inNum;
            param.setCurrentNumber(updateNum);

            //执行修改语句，根据id修改
            param.setId(result.getId());
            ryInventoryCurrentStockMapper.updateRyInventoryCurrentStock(param);
        } else {//不存在,执行新增
            param.setDepotId(depotId);
            param.setInventoryId(inventoryId);
            param.setCurrentNumber(ryDepotDetail.getOperNumber());

            //执行新增语句
            ryInventoryCurrentStockMapper.insertRyInventoryCurrentStock(param);
        }
        //入库记录新增
        RyDepotItem ryDepotItem = new RyDepotItem();
        ryDepotItem.setInventoryId(inventoryId);
        ryDepotItem.setDepotId(depotId);
        ryDepotItem.setOperNumber(ryDepotDetail.getOperNumber());
        ryDepotItem.setOperTime(DateUtils.getNowDate());
        ryDepotItem.setOperMan(user.getUserId());
        ryDepotItem.setPurchaseInId(ryDepotDetail.getPurchaseInId());//设置采购入库单id
        ryDepotItem.setHeaderType("0");//0代表入库
        //新增
        ryDepotItemMapper.insertRyDepotItem(ryDepotItem);

        //修改采购总清单明细表中总入库数量（已入库数量+本次入库操作数量）
        ryDepotDetail.setInNum(ryDepotDetail.getHavaInNum() + ryDepotDetail.getOperNumber());
        ryDepotDetailMapper.updateRyDepotDetail(ryDepotDetail);

        return AjaxResult.success("入库成功");
    }


    /**
     * 查询采购入库列表
     *
     * @param ryPurchaseIn 采购入库
     * @return 采购入库
     */
    @Override
    public List<RyPurchaseIn> selectRyPurchaseInList(RyPurchaseIn ryPurchaseIn)
    {
        return ryPurchaseInMapper.selectRyPurchaseInList(ryPurchaseIn);
    }

    @Override
    public List<RyDepotItem> selectItemForInByDepotId(Long id){
        return ryDepotItemMapper.selectItemForInByDepotId(id);
    }

    /**
     * 新增采购入库
     *
     * @param ryPurchaseIn 采购入库
     * @return 结果
     */
    @Override
    public int insertRyPurchaseIn(RyPurchaseIn ryPurchaseIn)
    {
        ryPurchaseIn.setCreateTime(DateUtils.getNowDate());
        return ryPurchaseInMapper.insertRyPurchaseIn(ryPurchaseIn);
    }

    /**
     * 修改采购入库
     *
     * @param ryPurchaseIn 采购入库
     * @return 结果
     */
    @Override
    public int updateRyPurchaseIn(RyPurchaseIn ryPurchaseIn)
    {
        return ryPurchaseInMapper.updateRyPurchaseIn(ryPurchaseIn);
    }

    /**
     * 批量删除采购入库
     *
     * @param ids 需要删除的采购入库主键
     * @return 结果
     */
    @Override
    public int deleteRyPurchaseInByIds(Long[] ids)
    {
        return ryPurchaseInMapper.deleteRyPurchaseInByIds(ids);
    }

    /**
     * 删除采购入库信息
     *
     * @param id 采购入库主键
     * @return 结果
     */
    @Override
    public int deleteRyPurchaseInById(Long id)
    {
        return ryPurchaseInMapper.deleteRyPurchaseInById(id);
    }
}
